package com.fy.market.strategy.service.rule.tree.impl;

import com.fy.market.strategy.model.entity.RuleMatterEntity;
import com.fy.market.strategy.model.enums.RuleLogicCheckTypeEnum;
import com.fy.market.strategy.model.entity.tree.RuleTreeResultEntity;
import com.fy.market.strategy.repository.IStrategyRepository;
import com.fy.market.strategy.service.rule.tree.ILogicTreeNode;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author fwq
 * @desc 兜底奖节点
 * @date 2025年09月25
 */
@Component("rule_luck_award")
public class RuleLuckAwardLogicTreeNode implements ILogicTreeNode {
    @Resource
    private IStrategyRepository iStrategyRepository;

    /**
     * 获取幸运奖品
     *
     * @param ruleMatterEntity
     * @return 幸运奖品id
     */
    @Override
    public RuleTreeResultEntity logic(RuleMatterEntity ruleMatterEntity) {
        Integer awardId = ruleMatterEntity.getAwardId();
        Integer guaranteedAwardId = ruleMatterEntity.getGuaranteedAwardId();
        Integer finalAwardId ;

        if (awardId != null || guaranteedAwardId != null)
            return RuleTreeResultEntity
                    .builder()
                    .ruleLogicCheckTypeEnum(RuleLogicCheckTypeEnum.ALLOW)
                    .awardId(guaranteedAwardId != null ? guaranteedAwardId : awardId)
                    .build();

        finalAwardId = iStrategyRepository.getStrategyEntity(ruleMatterEntity.getStrategyId()).getGuaranteedAwardId();

        return RuleTreeResultEntity
                .builder()
                .awardId(finalAwardId)
                .ruleLogicCheckTypeEnum(RuleLogicCheckTypeEnum.ALLOW) //模拟兜底奖，放行
                .build();
    }
}
